package com.incar.chuxing.activity;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.incar.chuxing.R;
import com.incar.chuxing.api.ApiStore;
import com.incar.chuxing.api.UrlConstant;
import com.incar.chuxing.base.BaseActivity;
import com.incar.chuxing.bean.QualificationBean;
import com.incar.chuxing.bean.DepositStatus;
import com.incar.chuxing.bean.WalletBean;
import com.incar.chuxing.bean.ReturnDepositBean;
import com.incar.chuxing.utils.Constants;
import com.incar.chuxing.utils.Dialogs.DepositDialog;
import com.incar.chuxing.utils.MyApplication;
import com.incar.chuxing.utils.SharedPreferencesKey;
import com.incar.chuxing.utils.SharedPreferencesUtils;
import com.incar.chuxing.utils.StatusBarUtil;
import com.incar.chuxing.utils.Toast.ToastCompat;
import com.umeng.analytics.MobclickAgent;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class WalletActivity extends BaseActivity {

    @BindView(R.id.return_myWallet)
    ImageView returnMyWallet;
    @BindView(R.id.wallet_group)
    RelativeLayout walletGroup;
    @BindView(R.id.wallet_group_line)
    View walletGroupLine;
    @BindView(R.id.wallet_single)
    LinearLayout walletSingle;
    @BindView(R.id.wallet_bike_deposit)
    RelativeLayout walletDeposit;
    @BindView(R.id.wallet_car_deposit)
    RelativeLayout walletCarDeposit;
    @BindView(R.id.wallet_coupon)
    RelativeLayout walletCoupon;
    //@BindView(R.id.wallet_fapiao_icon)
    //ImageView walletFapiaoIcon;
    //@BindView(R.id.wallet_fapiao)
    //AutoRelativeLayout walletFapiao;
    @BindView(R.id.activity_my_wallet)
    RelativeLayout activityMyWallet;
    @BindView(R.id.progress_bar_wallet)
    RelativeLayout progressBarWallet;
    @BindView(R.id.wallet_tv_jituan)
    TextView walletTvJituan;
    @BindView(R.id.wallet_tv_geren)
    TextView walletTvGeren;
    @BindView(R.id.wallet_tv_bike_bzj)
    TextView walletTvBikeBzj;
    @BindView(R.id.wallet_tv_car_bzj)
    TextView walletTvCarBzj;
    @BindView(R.id.wallet_tv_coupons)
    TextView walletTvCoupons;
    @BindView(R.id.charge_detail)
    TextView charge_detail;
    @BindView(R.id.my_wallet_recharge)
    TextView btToCharge;
    @BindView(R.id.wallet_tv_bike_isfree)
    TextView bikeFree;
    @BindView(R.id.wallet_bike_free)
    RelativeLayout walletBikeFree;

    private Context mContext = this;
    private int depositStatus;
    private double mBikeBail;
    private double mCarBail;

    private static final double CARBAIL = 599.0;
    private static final double BIKEBAIL = 299.0;

    @Override
    public int getLayoutId() {
        return R.layout.activity_wallet;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimaryWhite), 0, true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getMyWallet();
        getDepositStatus();
    }

    private void getMyWallet() {
        progressBarWallet.setVisibility(View.VISIBLE);
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).getWallet(((MyApplication) getApplication()).getRequestFieldMap()).enqueue(new Callback<WalletBean>() {
            @Override
            public void onResponse(Call<WalletBean> call, Response<WalletBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        progressBarWallet.setVisibility(View.GONE);
                        DecimalFormat df = new DecimalFormat("0.00");

                        mBikeBail = response.body().getAttachment().getBikeBail();
                        mCarBail = response.body().getAttachment().getCarBail();

                        walletTvJituan.setText(df.format(response.body().getAttachment().getEnterpriseBalance()) + "元");
                        walletTvGeren.setText(df.format(response.body().getAttachment().getBalance()));

                        if (df.format(mBikeBail).equals("0.00")) {
                            walletTvBikeBzj.setText(df.format(mBikeBail) + "元");
                        } else {
                            walletTvBikeBzj.setText(df.format(mBikeBail) + "元，退保证金");
                        }

                        if (df.format(mCarBail).equals("0.00")) {
                            walletTvCarBzj.setText(df.format(mCarBail) + "元");
                        } else {
                            walletTvCarBzj.setText(df.format(mCarBail) + "元，退保证金");
                        }

                        walletTvCoupons.setText(response.body().getAttachment().getCoupons() + "张");
                        int isFree = response.body().getAttachment().getIsBikeBailFree();
                        if (isFree == 1) {
                            bikeFree.setText("是");
                        } else {
                            bikeFree.setText("否");
                        }
                        if (response.body().getAttachment().getIsEnterprise() == 1) {
                            walletGroup.setVisibility(View.VISIBLE);
                            walletGroupLine.setVisibility(View.VISIBLE);
                        } else {
                            walletGroup.setVisibility(View.GONE);
                            walletGroupLine.setVisibility(View.GONE);
                        }
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<WalletBean> call, Throwable t) {
                progressBarWallet.setVisibility(View.GONE);
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void getDepositStatus() {
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).getDepositStatus(((MyApplication) getApplication()).getRequestFieldMap()).enqueue(new Callback<DepositStatus>() {
            @Override
            public void onResponse(Call<DepositStatus> call, Response<DepositStatus> response) {
                if (response.isSuccessful() && response.body() != null) {
                    depositStatus = response.body().getStatus();
                }
            }

            @Override
            public void onFailure(Call<DepositStatus> call, Throwable t) {
            }
        });
    }

    @OnClick({R.id.return_myWallet, R.id.wallet_group, R.id.wallet_single, R.id.wallet_bike_deposit, R.id.wallet_car_deposit, R.id.wallet_coupon, R.id.charge_detail, R.id.my_wallet_recharge})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.return_myWallet:
                finish();
                break;
            case R.id.wallet_group:
                break;
            case R.id.wallet_single:
                //startActivity(new Intent(WalletActivity.this, MyAccountActivity.class));
                break;

            case R.id.wallet_car_deposit:
                if (mCarBail != 0) {
                    final DepositDialog selfDialog = new DepositDialog(this);
                    selfDialog.setTitle(getString(R.string.dialogTitle));
                    selfDialog.setMessage(getString(R.string.returnDepositHasBail));
                    selfDialog.setYesOnclickListener("确定", new DepositDialog.onYesOnclickListener() {
                        @Override
                        public void onYesClick() {
                            returnDeposit("1");
                            selfDialog.dismiss();
                            MobclickAgent.onEvent(WalletActivity.this, Constants.CLICK_RETURN_OK);
                        }
                    });
                    selfDialog.setNoOnclickListener("取消", new DepositDialog.onNoOnclickListener() {
                        @Override
                        public void onNoClick() {
                            selfDialog.dismiss();
                        }
                    });
                    selfDialog.show();
                    MobclickAgent.onEvent(WalletActivity.this, Constants.CARBAIL_REFUND);
                } else {
                    checkCarUserStatusAndIntent();
                    MobclickAgent.onEvent(WalletActivity.this, Constants.CARBAIL_CHARGE);
                }
                break;
            case R.id.wallet_coupon:
                startActivity(new Intent(WalletActivity.this, CouponsActivity.class));
                break;
           /* case R.id.wallet_fapiao:
                startActivity(new Intent(WalletActivity.this,MyInvoiceActivity.class));
                break;*/
            case R.id.charge_detail:
                Intent intent = new Intent(mContext, RechargeRecordActivity.class);
                startActivity(intent);
                break;
            case R.id.my_wallet_recharge:
                startActivity(new Intent(WalletActivity.this, RechargeActivity.class));
                break;
        }
    }

    private void checkCarUserStatusAndIntent() {
        progressBarWallet.setVisibility(View.VISIBLE);
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).getUserStatus(((MyApplication) getApplication()).getRequestFieldMap()).enqueue(new Callback<QualificationBean>() {
            @Override
            public void onResponse(Call<QualificationBean> call, Response<QualificationBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    progressBarWallet.setVisibility(View.GONE);
                    if (response.body().getStatus() == 200) {

                        SharedPreferencesUtils.getInstance(mContext).putStringValue(SharedPreferencesKey.REAL_NAME_STATUS, response.body().getAttachment().getIdentityStatus() + "");
                        SharedPreferencesUtils.getInstance(mContext).putStringValue(SharedPreferencesKey.LICENSE_CAR_STATUS, response.body().getAttachment().getLicenseStatus() + "");

                        Intent intent = new Intent(WalletActivity.this, QualificationActivity.class);
                        intent.putExtra("SubmitFlag", 3);
                        startActivity(intent);
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<QualificationBean> call, Throwable t) {
                progressBarWallet.setVisibility(View.GONE);
                ToastCompat.makeText(mContext, mContext.getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
                Log.i("neterror", "008");
            }
        });
    }

    private void returnDeposit(String bailType) {
        progressBarWallet.setVisibility(View.VISIBLE);
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).returnDeposit(((MyApplication) getApplication()).getRequestFieldMap(), bailType).enqueue(new Callback<ReturnDepositBean>() {
            @Override
            public void onResponse(Call<ReturnDepositBean> call, Response<ReturnDepositBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    progressBarWallet.setVisibility(View.GONE);
                    ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_LONG).show();
                    //退完后刷新一下
                    getMyWallet();
                } else {
                    progressBarWallet.setVisibility(View.GONE);
                    ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ReturnDepositBean> call, Throwable t) {
                progressBarWallet.setVisibility(View.GONE);
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
            }
        });
    }

}
